package com.lzd.exception;



import com.lzd.pojo.response.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class ExceptionCatch {
    private static Map<String ,String > map =new HashMap<String ,String >();

    static {
        map.put("java.lang.ArithmeticException","分母不能为0异常");
        map.put("java.lang.NullPointerException","发生空指针异常，请联系技术人员");
        map.put("org.apache.shiro.authz.UnauthenticatedException","当前用户没有访问权限");
        map.put("org.apache.shiro.authz.UnauthorizedException","当前用户没有访问权限");
    }
    @ExceptionHandler
    @ResponseBody
    public ResponseResult catchException(Exception exception){
        exception.printStackTrace();
        String name = exception.getClass().getName();
        if(name.equals("com.lzd.exception.RunException")){
            com.lzd.exception.RunException ex= (com.lzd.exception.RunException) exception;
            return new ResponseResult(00000,ex.getMessage(),null);
        }
        String message = map.get(name);
        if(message==null){
            message="出现未知异常"+name;
        }
        return new ResponseResult(00000,message,null);
    }
}
